package cn.regexp.web.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.regexp.domain.ShopBanner;
import cn.regexp.service.IBannerService;
import cn.regexp.utils.JsonResult;
import cn.regexp.utils.RedisKeys;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("banners")
public class BannerController {
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/queryBanner")
    public JsonResult<List<ShopBanner>> queryBanner() {
        String bannerStr = redisTemplate.opsForValue().get(RedisKeys.BANNER_INFO.join());
        List<ShopBanner> bannerList;
        if (ObjectUtil.isNotEmpty(bannerStr)) {
            bannerList = JSON.parseArray(bannerStr, ShopBanner.class);
        } else {
            bannerList = bannerService.queryBanner();
        }
        return  JsonResult.success(bannerList);
    }

}
